package soot.we.android.component;

import java.util.HashSet;
import java.util.Set;

import soot.Local;
import soot.Unit;
import soot.Value;
import soot.ValueBox;
import soot.toolkits.graph.DirectedGraph;
import soot.toolkits.scalar.BackwardFlowAnalysis;

public class LiveVariablesAnalysis extends BackwardFlowAnalysis<Unit, Set> {

	public LiveVariablesAnalysis(DirectedGraph<Unit> graph) {
		super(graph);
		doAnalysis();
	}

	@Override
	protected void flowThrough(Set src, Unit ut, Set dest) {
		for (ValueBox box : ut.getDefBoxes()) {
			Value value = box.getValue();
			if (value instanceof Local)
				dest.remove(value);
		}
		for (ValueBox box : ut.getUseBoxes()) {
			Value value = box.getValue();
			if (value instanceof Local)
				dest.add(value);
		}
	}

	@Override
	protected void copy(Set src, Set dest) {
		dest.clear();
		dest.addAll(src);
	}

	@Override
	protected Set entryInitialFlow() {
		// TODO Auto-generated method stub
		return new HashSet();
	}

	@Override
	protected void merge(Set src1Set, Set src2Set, Set dest) {
		dest.clear();
		dest.addAll(src1Set);
		dest.addAll(src2Set);

	}

	@Override
	protected Set newInitialFlow() {
		// TODO Auto-generated method stub
		return new HashSet();
	}

}
